﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://www.wallpaper-control.xe.cx/ and 
// http://wpcontrol.codeplex.com/ for more.
//
// Copyright (c) 2009 Niklas Kroll
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.IO;
using System.Windows.Forms;

namespace wpControl1
{
    /// <summary>
    /// Manages the windows wallpaper including setting the next, the previous and
    /// the selected wallpaper as desktop background.
    /// </summary>
    class WallpaperManager
    {
        /// <summary>
        /// The index of the next wallpaper in the list. Starting with index 0 
        /// (first item).
        /// </summary>
        internal int NextWallpaperIndex;
        /// <summary>
        /// The index of the previous wallpaper in the list.
        /// </summary>
        internal int PreviousWallpaperIndex;

        /// <summary>
        /// Constructor that loads the default values if a new manager instance is 
        /// started.
        /// </summary>
        internal WallpaperManager()
        {
            this.SetDefaultValues();
        }

        /// <summary>
        /// Sets the default values so that the next wallpaper is the first image.
        /// </summary>
        internal void SetDefaultValues()
        {
            this.NextWallpaperIndex = 0;
            // Hmm, shouldn't that be NexWallpaperIndex - 2 ??
            this.PreviousWallpaperIndex = NextWallpaperIndex - 1;
        }

        /// <summary>
        /// Sets the selected wallpaper as background.
        /// </summary>
        internal void SetSelectedWallpaper()
        {
            try
            {
                // Get selected image index
                int index = Program.ControlPanel.listBoxImages.SelectedIndex;
                // Change wallpaper
                Program.Wallpaper.SetWallpaper(Program.ImageManager.Images[index], Program.ZoomStyleManager.Styles[index]);
                // Update indices
                this.NextWallpaperIndex = index + 1;
                this.PreviousWallpaperIndex = index - 1;
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SetWallpaper_Argument"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SetWallpaper_NotFound"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Sets the wallpaper by its index in the image list
        /// </summary>
        /// <param name="index">The zero-based index of the image in the list of images.</param>
        internal void SetWallpaper(int index)
        {
            try
            {
                // Change wallpaper
                Program.Wallpaper.SetWallpaper(Program.ImageManager.Images[index], Program.ZoomStyleManager.Styles[index]);
                // Update indices
                this.NextWallpaperIndex = index + 1;
                this.PreviousWallpaperIndex = index - 1;
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SetWallpaper_Argument"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SetWallpaper_NotFound"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Sets the previous wallpaper as background.
        /// </summary>
        internal void SetPreviousWallpaper()
        {
            try
            {
                //PreviousWallpaperIndex = NextWallpaperIndex - 2;
                if (PreviousWallpaperIndex >= 0)
                {
                    // Change wallpaper
                    Program.Wallpaper.SetWallpaper(Program.ImageManager.Images[PreviousWallpaperIndex], Program.ZoomStyleManager.Styles[PreviousWallpaperIndex]);
                    // Keep indices up-to-date
                    this.NextWallpaperIndex--;
                    this.PreviousWallpaperIndex--;
                }
                /*else
                {
                    // No image with index lower than 0 - select first wallpaper
                    PreviousWallpaperIndex = 0;
                }*/
                // Keep schedule up-to-date
                Program.ControlPanel.UpdateSchedule();
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SetWallpaper_NotFound"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SetWallpaper_Argument"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Sets the next wallpaper as background.
        /// </summary>
        internal void SetNextWallpaper()
        {
            try
            {
                // Check if the last wallpaper is reached
                if (Program.WallpaperManager.NextWallpaperIndex < Program.ControlPanel.listBoxImages.Items.Count)
                {
                    // Change wallpaper
                    Program.Wallpaper.SetWallpaper(Program.ImageManager.Images[NextWallpaperIndex], Program.ZoomStyleManager.Styles[NextWallpaperIndex]);
                    // Update indices
                    this.NextWallpaperIndex++;
                    this.PreviousWallpaperIndex++;
                    // Show balloon tip if requested
                    if (Program.SettingsManager.ChangeInfo)
                    {
                        if (Program.TrayApp.Active)
                        {
                            Program.TrayApp.TrayIcon.ShowBalloonTip(2000, "WallpaperControl", Program.LanguageManager.GetString("Tip_WallpaperChanged"), ToolTipIcon.Info);
                        }
                    }
                }
                // Last wallpaper reached
                else
                {
                    // If loop, begin again with first image.
                    if (Program.SettingsManager.LoopWallpapers)
                    {
                        Program.WallpaperManager.NextWallpaperIndex = 0;
                        // Show balloon tip if requested
                        if (Program.SettingsManager.LoopingInfo)
                        {
                            if (Program.TrayApp.Active)
                            {
                                Program.TrayApp.TrayIcon.ShowBalloonTip(2000, "WallpaperControl", Program.LanguageManager.GetString("Tip_WallpaperLooping"), ToolTipIcon.Info);
                            }
                        }
                    }
                    else
                    {
                        // Stop session
                        Program.ChangeClient.StopSession();
                    }
                }
                // Keep schedule up-to-date
                Program.ControlPanel.UpdateSchedule();
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SetWallpaper_NotFound"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SetWallpaper_Argument"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
